package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.dao.ArticleTagMapper;
import com.dmap.knowledge.po.ArticleTag;
import com.dmap.knowledge.po.ArticleTagExample;
import com.dmap.knowledge.service.ArticleTagService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ArticleTagServiceImpl implements ArticleTagService {
    @Resource
    private ArticleTagMapper articleTagMapper;
    @Override
    public List<ArticleTag> selectByArticleId(long id) {
        ArticleTagExample articleTagExample = new ArticleTagExample();
        articleTagExample.createCriteria().andArticleIdEqualTo(id);
        return articleTagMapper.selectByExample(articleTagExample);
    }
    @Override
    public int insertArticleTag(long articleId, long tagTypeId, String tagTypeName, long tagId, String tagName, long createTime) {
        ArticleTag articleTag = new ArticleTag();
        articleTag.setArticleId(articleId);
        articleTag.setTagTypeId(tagTypeId);
        articleTag.setTagTypeName(tagTypeName);
        articleTag.setTagName(tagName);
        articleTag.setCreateTime(createTime);
        articleTag.setTagId(tagId);
        return articleTagMapper.insertSelective(articleTag);
    }
    @Override
    public int deleteArticleTagByArticleId(long articleId) {
        ArticleTagExample articleTagExample = new ArticleTagExample();
        articleTagExample.createCriteria().andArticleIdEqualTo(articleId);
        //如果没有就返回1
        if (CollectionUtils.isEmpty(articleTagMapper.selectByExample(articleTagExample))) {
            return 1;
        }
        return articleTagMapper.deleteByExample(articleTagExample);
    }

    @Override
    public Integer countArticleNumberByTagId(long tagId) {
        ArticleTagExample articleTagExample = new ArticleTagExample();
        articleTagExample.createCriteria().andTagIdEqualTo(tagId);
        return articleTagMapper.countByExample(articleTagExample);
    }
}
